package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Member;
import cn.wolfcode.cms.query.MemberQueryObject;
import cn.wolfcode.cms.service.*;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//会员
@Controller
@RequestMapping("member")
public class MemberController {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private IMemberGradeService memberGradeService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private IMembernumService membernumService;

    @RequiresPermissions("member:index")
    @PermissionName("会员首页")
    @RequestMapping("")
    public String index() {
        memberService.list();
        return "member/list";
    }

    //会员信息列表/带分页
    @RequiresPermissions("member:list")
    @PermissionName("会员首页")
    @RequestMapping("list")
    @ResponseBody
    public Object list(MemberQueryObject qo) {
        return memberService.query(qo);
    }


    //所有会员信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {
        return memberService.list();
    }


    //会员添加
    @RequiresPermissions("member:add")
    @PermissionName("会员添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Member entity) {
        JSONResult result = new JSONResult();
        try {
            memberService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //会员编辑
    @RequiresPermissions("member:edit")
    @PermissionName("会员编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Member entity) {
        JSONResult result = new JSONResult();
        try {
            memberService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    //会员删除
    @RequiresPermissions("member:delete")
    @PermissionName("会员删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            memberService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }

    //获取所有等级
    @RequestMapping("getAllGrade")
    @ResponseBody
    public Object getAllGrade() {
        return memberGradeService.list();
    }

    //获取所有分组
    @RequestMapping("getAllGroup")
    @ResponseBody
    public Object getAllGroup() {
        return memberGroupService.list();
    }

    //通过id获取所有member关联对象
    @RequestMapping("getMemberByExchangeId")
    @ResponseBody
    public Object getMemberByExchangeId(Long id) {
        List<Member> memberByExchangeId = memberService.getMemberByExchangeId(id);
        return memberByExchangeId;
    }

    //会员删除
    @RequiresPermissions("member:moveMember")
    @PermissionName("移动分组")
    @RequestMapping("moveMember")
    @ResponseBody
    public Object moveMember(Long memberId, Long groupId) {
        JSONResult result = new JSONResult();
        try {
            memberService.moveMember(memberId, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }


    //----------------------------------------------------------------
    //获取会员总数
    @RequestMapping("getTotalNum")
    @ResponseBody
    public Object getTotalNum() {
        return memberService.getTotalNum();
    }

    //获取今天过生日的会员总数 以及本月过生日会员
    @RequestMapping("getBrithMenBer")
    @ResponseBody
    public Object getBrithMenBer() {
        return memberService.getBrithMenBer();
    }

    //获取累计总消费
    @RequestMapping("getTotalSpend")
    @ResponseBody
    public Object getTotalSpend() {
        return memberService.getTotalSpend();
    }

    //获取累计消费最高的前三名
    @RequestMapping("getMax")
    @ResponseBody
    public Object getMax() {
        return memberService.getMax();
    }
    //---------------------------------------------------------------

    //通过关键字 获取单个会员信息 用于会员充值
    @RequestMapping("getMemByKeyInfo")
    @ResponseBody
    public Object getMemByKeyInfo(String keyInfo) {
        return memberService.getMemByKeyInfo(keyInfo);
    }

    //通过卡号查找会员信息  用于冲次
    @RequestMapping("getMemberInfoByCardNum")
    @ResponseBody
    public Object getMemberInfoByCardNum(String cardnum) {
        return memberService.getMemberInfoByCardNum(cardnum);
    }

    //获取累计总消费 以及平均消费//暂时未写 需要消费列表
    @RequestMapping("getRecordBykeyword")
    @ResponseBody
    public Object getRecordBykeyword(String keyword) {
        return memberService.getRecordBykeyword(keyword);
    }
}
